package com.ruoyi.mdm.controller;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.mdm.domain.dto.MaterialCategoryDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mdm.domain.MaterialCategory;
import com.ruoyi.mdm.service.IMaterialCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料类别Controller
 *
 * @author ruoyi
 * @date 2024-03-27
 */
@RestController
@RequestMapping("/mdm/materialCategory")
public class MaterialCategoryController extends BaseController {
	@Resource
	private IMaterialCategoryService materialCategoryService;

	/**
	 * 查询物料类别列表
	 */
	@PreAuthorize("@ss.hasPermi('mdm:materialCategory:list')")
	@GetMapping("/list")
	public AjaxResult list(MaterialCategory materialCategory) {
		List<MaterialCategory> list = materialCategoryService.selectMaterialCategoryList(materialCategory);
		return success(list);
	}

	/**
	 * 导出物料类别列表
	 */
	@PreAuthorize("@ss.hasPermi('mdm:materialCategory:export')")
	@Log(title = "物料类别", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, MaterialCategory materialCategory) {
		List<MaterialCategory> list = materialCategoryService.selectMaterialCategoryList(materialCategory);
		ExcelUtil<MaterialCategory> util = new ExcelUtil<MaterialCategory>(MaterialCategory.class);
		util.exportExcel(response, list, "物料类别数据");
	}

	/**
	 * 获取物料类别详细信息
	 */
	@PreAuthorize("@ss.hasPermi('mdm:materialCategory:query')")
	@GetMapping(value = "/{tableId}")
	public AjaxResult getInfo(@PathVariable("tableId") Long tableId) {
		return success(materialCategoryService.selectMaterialCategoryByTableId(tableId));
	}

	/**
	 * 新增物料类别
	 */
	@PreAuthorize("@ss.hasPermi('mdm:materialCategory:add')")
	@Log(title = "物料类别", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MaterialCategory materialCategory) {
		return toAjax(materialCategoryService.insertMaterialCategory(materialCategory));
	}

	/**
	 * 修改物料类别
	 */
	@PreAuthorize("@ss.hasPermi('mdm:materialCategory:edit')")
	@Log(title = "物料类别", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MaterialCategory materialCategory) {
		return toAjax(materialCategoryService.updateMaterialCategory(materialCategory));
	}

	/**
	 * 删除物料类别
	 */
	@PreAuthorize("@ss.hasPermi('mdm:materialCategory:remove')")
	@Log(title = "物料类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tableIds}")
	public AjaxResult remove(@PathVariable Long[] tableIds) {
		return toAjax(materialCategoryService.deleteMaterialCategoryByTableIds(tableIds));
	}

	@PreAuthorize("@ss.hasPermi('mdm:materialCategory:import')")
	@Log(title = "导入物料类别", businessType = BusinessType.IMPORT)
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws IOException {
		ExcelUtil<MaterialCategoryDTO> util = new ExcelUtil<>(MaterialCategoryDTO.class);
		List<MaterialCategoryDTO> materialCategoryList = util.importExcel(file.getInputStream());
		return success(materialCategoryService.importData(materialCategoryList, updateSupport));
	}

	@PostMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response) {
		ExcelUtil<MaterialCategoryDTO> util = new ExcelUtil<>(MaterialCategoryDTO.class);
		util.importTemplateExcel(response, "物料分类");
	}
}
